<?php
//***@#$ 2018-10-22 10:35 by lmx | 快速专题系统对接接口
require '../common.inc.php';
require_once DT_ROOT.'/include/post.func.php';
//***@#$ 2018-11-06 15:40 by lmx | 限制内网访问
$ip = $_SERVER['REMOTE_ADDR'];
$flag='0';
foreach($CFG['intranet'] as $theIipAarea){
    if(strpos($ip, $theIipAarea) !== false){
        $flag='1';
        break;
    }
}
if($flag!=='1'){
    die('非法访问。');
}
//***@#$ 2018-11-06 15:40 by lmx | 限制内网访问
$session = new dsession();
switch ($action){
    case 'login'://1.登录
        if(!check_name($username) ||(strlen($password) < 6 || strlen($password) > 32)){
            $return='0';
        }else{
            $MOD = cache_read('module-2.php');
            require DT_ROOT.'/include/module.func.php';
            require DT_ROOT.'/module/member/member.class.php';
            $secretkey = 'a'.strtolower(substr(md5(DT_KEY), -8));
            $do = new member;
            $user = $do->login($username, $password);//var_dump($user);
            if($user) {
                if($user['groupid'] != 1 || $user['admin'] < 1) $return='-2';//无权访问后台
                if(!is_founder($user['userid'])) {
                    if(($DT['admin_week'] && !check_period(','.$DT['admin_week'])) || ($DT['admin_hour'] && !check_period($DT['admin_hour']))) $return='-3';//未被允许的管理时间
                }
                $info=array_slice($user,0,2);//主要信息暂存
                $session = new dsession();
                $_SESSION[$secretkey] = $info;
                $return=$secretkey;
            } else {
                $return='-1';
            }
        }
        echo $callback?$callback."(".json_encode($return).")":json_encode($return);
        break;
    case 'logout'://2.退出
        session_destroy();
        $return='1';
        echo $callback?$callback."(".json_encode($return).")":json_encode($return);
        break;
    case 'special_list'://3.专题列表
        if($_SESSION[$secretkey]!==null){
            $username=$_SESSION[$secretkey]['username'];
            $table=DT_PRE."special_".$moduleid;
            $pagesize=10;
            $return=array();
            $result = DB::query("SELECT itemid,title,addtime FROM {$table} WHERE status=3 and username='{$username}' ORDER BY addtime desc LIMIT $pagesize", "CACHE");
            while($r = DB::fetch_array($result)) {
                array_push($return,$r);
            }
        }else{
            $return='-1';
        }
        echo $callback?$callback."(".json_encode($return).")":json_encode($return);
        break;
    case 'type_list'://4.分类列表
        if($_SESSION[$secretkey]!==null){
            $return=array();
            $result = DB::query("SELECT * FROM ".DT_PRE."type WHERE item='special-$specialid' ORDER BY listorder ASC,typeid ASC","CACHE");
            while($r = DB::fetch_array($result)) {
                array_push($return,$r);
            }
        }else{
            $return='-1';
        }
        echo $callback?$callback."(".json_encode($return).")":json_encode($return);
        break;
    case 'item_list'://5.文档列表
        if($_SESSION[$secretkey]!==null){
            $table=DT_PRE."special_item_".$moduleid;
            $pagesize=$pagesize?$pagesize:10;
            $return=array();
            $result = DB::query("SELECT itemid,title,style,introduce,thumb,addtime,linkurl FROM {$table} WHERE specialid={$specialid} and typeid={$typeid} ORDER BY addtime desc LIMIT $pagesize", "CACHE");
            while($r = DB::fetch_array($result)) {
                array_push($return,$r);
            }
        }else{
            $return='-1';
        }
        echo $callback?$callback."(".json_encode($return).")":json_encode($return);
        break;
    case 'template_save'://6.模板保存
        if($_SESSION[$secretkey]!==null) {
            $dir = DT_ROOT.'/template/'.$CFG['template'] . '/special/';
            $name = "show-kszt{$specialid}_{$name}";
            $table=DT_PRE."special_".$moduleid;
            if(isset($_FILES['template'])){
                if(move_uploaded_file($_FILES['template']['tmp_name'],$dir.$name.'.htm')){
                    DB::query("UPDATE {$table} SET template='{$name}' WHERE itemid IN ($specialid)");
                    $return='1';
                }else{
                    $return='0';
                }
            }else{
                $return='-2';
            }
        }else{
            $return='-1';
        }
        echo $callback?$callback."(".json_encode($return).")":json_encode($return);
        break;
}